package com.vieewer.btdemo.utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.vieewer.btdemo.view.main.HomeGuideActivity;

public class PermissionUtil {

    public static void requestPermissions(Activity activity) {

        RxPermissions rxPermission = new RxPermissions(activity);
        rxPermission
                .requestEach(
                        Manifest.permission.CAMERA,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CALL_PHONE,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.READ_EXTERNAL_STORAGE

                ).subscribe(permission -> {
                    if (permission.granted) {
                        // 用户已经同意该权限
                        Log.d("yjj", permission.name + " is granted.");
                    } else if (permission.shouldShowRequestPermissionRationale) {
                        // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                        Log.d("yjj", permission.name + " is denied. More info should be provided.");
                    } else {
                        // 用户拒绝了该权限，并且选中『不再询问』
                        Log.d("yjj", permission.name + " is denied.");
                    }


                });

    }


    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    public static void checkPermission(Activity activity) {
        //检查权限（NEED_PERMISSION）是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //用户已经拒绝过一次，再次弹出权限申请对话框需要给用户一个解释
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission
                    .READ_EXTERNAL_STORAGE)) {
                Toast.makeText(activity, "请开通相关权限，否则无法正常使用本应用！", Toast.LENGTH_SHORT).show();
            }
            //申请权限
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE);

        } else {
            //Toast.makeText(activity, "授权成功！", Toast.LENGTH_SHORT).show();
            Log.e("yjj", "checkPermission: 已经授权！");
        }
    }











}
